#include "manager.h"
#include "ui_manager.h"

Manager::Manager(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Manager)
{
    ui->setupUi(this);

    queryWidget=nullptr;
    entityEditor=nullptr;
    logViewer=new LogViewer(this);
    connect(this,&Manager::appendLog,logViewer,&LogViewer::appendLog);
}

Manager::~Manager()
{
    delete ui;
}

void Manager::showMessage(const QString &message)
{
    ui->statusbar->showMessage(message,6000);
}

void Manager::transferLog(const QString &log)
{
    emit appendLog(log);
}

void Manager::prepareQueryWidget(int idx)
{
    if(queryWidget == nullptr)
    {
        queryWidget=new QueryWidget(this);
        connect(queryWidget,&QueryWidget::showMessage,
                this,&Manager::showMessage);
        connect(queryWidget,&QueryWidget::transferLog,
                this,&Manager::transferLog);
    }
    if(centralWidget() != queryWidget)
        setCentralWidget(queryWidget);

    queryWidget->prepare(idx);
}

void Manager::on_actionDepartment_triggered()
{
    prepareQueryWidget(IDX_Department);
}

void Manager::on_actionPerson_triggered()
{
    prepareQueryWidget(IDX_Person);
}

void Manager::on_actionSaleOrder_triggered()
{
    prepareQueryWidget(IDX_SaleOrder);
}

void Manager::on_actionEntityEditor_triggered()
{
    if(entityEditor == nullptr)
        entityEditor=new EntityEditor(this);
    entityEditor->show();
}

void Manager::on_actionLogViewer_triggered()
{
    if(logViewer->isHidden())
        logViewer->show();
}
